using System;
using System.Collections;
using System.Threading;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Framework;

namespace Mn.Framework.UI.Dnn
{
    [Serializable]

    #region Class DnnModuleBase

    public class MnModuleBase : PortalModuleBase, IActionable
    {
        #region "Private Vars"

        private readonly ModuleController _moduleController = new ModuleController();

        #endregion

        protected ModuleActionCollection MyActions = new ModuleActionCollection();

    #endregion

        public CDefault BasePage
        {
            get
            {
                try
                {
                    return (CDefault)Page;
                }
                catch
                {
                    return null;
                }
            }
        }

        public void SetModuleConfiguration(ModuleInfo config)
        {
            ModuleConfiguration = config;
        }

        protected virtual void Page_Init(Object sender, EventArgs e)
        {
            try
            {
            }
            catch (ThreadAbortException taexp)
            {
            }
            catch (Exception exp)
            {
            }
        }

        #region Optional Interfaces

        public virtual ModuleActionCollection ModuleActions
        {
            get { return MyActions; }
        }

        public string ReadSetting(string setting)
        {
            setting = setting.Trim().ToLower();
            try
            {
                Hashtable ht = _moduleController.GetModuleSettings(ModuleId);
                if (ht[setting] != null)
                {
                    return (string)ht[setting];
                }
            }
            catch
            {
                return "";
            }
            return "";
        }

        public void SaveSetting(string setting, string settingValue)
        {
            setting = setting.Trim().ToLower();
            try
            {
                _moduleController.UpdateModuleSetting(ModuleId, setting, settingValue);
            }
            catch (Exception)
            {
            }
        }

        #endregion
    }
}